import Vue from 'vue';
import store from 'store';
import sessStorage from 'store/storages/sessionStorage';

const sessionStorage = store.createStore(sessStorage);

const PREFIX = 'lj-lvtong-h5';

export const storage = {
  get: (key) => {
    return store.get(`${PREFIX}/${key}`);
  },
  set: (key, ...args) => {
    return store.set(`${PREFIX}/${key}`, ...args);
  },
  remove: (key) => {
    return store.remove(`${PREFIX}/${key}`);
  },
  clearAll: () => store.clearAll(),
};

export const session = {
  get: (key) => {
    return sessionStorage.get(`${PREFIX}/${key}`);
  },
  set: (key, ...args) => {
    return sessionStorage.set(`${PREFIX}/${key}`, ...args);
  },
  remove: (key) => {
    return sessionStorage.remove(`${PREFIX}/${key}`);
  },
  clearAll: () => sessionStorage.clearAll(),
};

Object.defineProperties(Vue.prototype, {
  $storage: {
    get: () => storage,
  },
  $session: {
    get: () => session,
  },
});

const plugin = (cxt, inject) => {
  cxt.$storage = storage;
  cxt.$session = session;
};

export default plugin;
